var CmsPhotoListControl = Class.create(GridControl, {
    mainPhoto:null,
    photos:null,
    hashKey: null,

    observeEvent: function($super)
    {
        $super();
        this.hashKey = 'photo';
        var me = this;
        this.photos = this.find('.thumb_photo');
        this.photos.invoke('observe', 'click', function(){
            me.setHash(me.hashKey, this.id);
        });
    },

    setDefaultPhoto: function(id)
    {
        this.setParam('param[defaultPhoto]', id);
        this.find('.thumb_photo.default').invoke('removeClassName', 'default');
        $(id).addClassName('default');
    },
	
    getFirstPhoto: function()
    {
        return this.photos.first();
    },

    getLastPhoto: function()
    {
        return this.photos.last();
    },

    getSelected: function()
    {
        var selected = this.findOne('.thumb_photo.selected');
        if (!selected) selected = this.getFirstPhoto();
        return selected;
    },

    getNextPhoto: function()
    {
        return this.getSelected().next('.thumb_photo');
    },

    getPreviousPhoto: function()
    {
        return this.getSelected().previous('.thumb_photo');
    },

    selectPhoto: function(photo)
    {
        this.find('.thumb_photo.selected').invoke('removeClassName', 'selected');
        photo.addClassName('selected');
        this.loadPhoto(photo.id);
    },

	getMainPhoto: function()
	{
		if (this.mainPhoto) return this.mainPhoto;
		else
		{
			this.mainPhoto = $('mainphoto');
            this.mainPhoto.observe('click', this.selectNext.bind(this));
		}
		return this.mainPhoto;
	},

    loadPhoto: function(id)
    {
        this.fire('loadPhoto', id);
    },

    onHashChange: function($super, newHash)
    {
        $super(newHash);
        var photo = $(this.getHash(this.hashKey, newHash));
        if (photo) this.selectPhoto(photo);
    },

    onAjaxComplete: function()
    {
        var id = this.getHash(this.hashKey, getLocationHash());
        if (!id) this.selectPhoto(this.getFirstPhoto());
        else if (id == 'first') this.selectFirstPhoto();
        else if (id == 'last') this.selectLastPhoto();
        else
        {
            var photo = $(id);
            if (photo) this.selectPhoto(photo);
            else this.loadPhoto(id);
        }
    },

    selectFirstPhoto: function()
    {
        var photo = this.getFirstPhoto();
        this.setHash(this.hashKey, photo.id);
    },

    selectLastPhoto: function()
    {
        var photo = this.getLastPhoto();
        this.setHash(this.hashKey, photo.id);
    },

    selectNext: function()
    {
        var photo = this.getNextPhoto();
        if (photo) this.setHash(this.hashKey, photo.id);
        else
        {
            this.nextPage();
            this.setHash(this.hashKey, 'first');
        }
    },

    selectPrevious: function()
    {
        var photo = this.getPreviousPhoto();
        if (photo) this.setHash(this.hashKey, photo.id);
        else
        {
            this.prevPage();
            this.setHash(this.hashKey, 'last');
        }
    }
});